/*
*选择联系人
示例
var ContactWin;
 function selContact(){
  if(!ContactWin){
 ContactWin= new ContactSel();
 ContactWin.on('onSelectItem', function(data) {
      form.getForm().findField("customerId").setValue(data.id);
      form.getForm().findField("customerName").setValue(data.name);
    }, this);
 }
 ContactWin.show();
 }
* */
Ext.define("App.select.contactSel",{
    extend : 'Ext.window.Window',
    alias : 'widget.contactSel',
    layout : 'fit',
    autoScroll : true,
    autoHeight : true,
    width:600,
    height:500,
    customersId:null,
    title:'联系人选择',
    initComponent: function() {
       var searchFlds = new Ext.util.MixedCollection();
        searchFlds.add('name', new Ext.form.TextField({
            name: 'name',
            dataIndex: 'name',
            fieldLabel: '联系人',
            allowBlank:false
        }));
       this.grid = Ext.create('gridView', {
            remoteSort: true,
            gridId: 'gridContactSel',
            checkboxSelectionModel:     true,
           dataAutoLoad : false,
           searchFlds: searchFlds,
           entityClassName : 'com.rover.crm.entity.Contact',
           queryUrl : 'contactController/query.action',
            deleteUrl:null,
            recordPk: [ "id" ],
           tbar:[
               searchFlds.get('name'),
               {xtype:'button',text:'查询',id:'btn_Query',iconCls:'btn_edit',handler:function(){
                   Ext.getCmp("gridContactSel").executeQuery();
               }}
           ],
           listeners : {
            itemdblclick : function(grid, rowindex, e) {
                Ext.getCmp("gridContactSel").selectItem();
            }
        }
        });
        this.customersId =  this.customersId && 0;
        this.grid.getStore().on('beforeload', function(store, options) {
            store.proxy.extraParams.customersId = this.customersId;
        }, this, true);
        this.grid.load();
        this.items = [this.grid];
        this.layout= 'fit';
        this.modal = true; // 是否为模态窗口
        this.resizable = false; // 是否可以改变窗口大小
        this.buttonAlign= 'center',
        this.buttons = [{
            iconCls : "btn-ok",
            text : "确定",
            scope : this,
            handler : this.selectItem
        }, {
            text : "取消",
            iconCls : "btn-cancel",
            handler : this.onWinClose,
            scope : this
        }];
        this.addEvents({// 加入自定义事件
            "onSelectItem" : true
        });
        this.callParent(arguments);
    },
    selectItem : function() {
        var sm = this.grid.getSelectionModel();
        var records = sm.getSelection();
        if (records.length <= 0) {
            Ext.Msg.alert('提示', "未选中记录！");
            return;
        }
        this.fireEvent('onSelectItem', records[0].raw);// 激发自定义事件
        this.close();

    },
    onWinClose : function() {
        this.close();
    }
});